﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ProjectVLIB_LAB04.KetNoiDB;
using System.Data.SqlClient;
using ProjectVLIB_LAB04.VOL;

namespace ProjectVLIB_LAB04.BLL
{
    class NhaCungCapBL
    {
        private DataAccess dataAccess;

        public NhaCungCapBL()
        {
            dataAccess = new DataAccess();
        }

        /// <summary>
        /// 1. Hiển thị danh sách nha cung cap
        /// </summary>
        /// <returns></returns>
        public DataTable GetData()
        {
            // cau query select
            string sql = "SELECT MaNhaCungCap, TenNhaCungCap, DiaChi, DienThoai, Email FROM tbl_NhaCungCap ORDER BY MaNhaCungCap DESC ";

            DataTable dt = new DataTable();
            // do toan bo du lieu vao datatable
            dt = dataAccess.GetData(sql);
            return dt;
        }

        /// <summary>
        /// 2. Thêm nha cung cap
        /// </summary>
        /// <param name="obj">NhaCungCap</param>
        /// <returns>true</returns>
        public bool InsertData(NhaCungCap obj)
        {
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@TenNhaCungCap", SqlDbType.NVarChar) { Value = obj.TenNhaCungCap };
            param[1] = new SqlParameter("@DiaChi", SqlDbType.NVarChar) { Value = obj.DiaChi };
            param[2] = new SqlParameter("@DienThoai", SqlDbType.NVarChar) { Value = obj.DienThoai };
            param[3] = new SqlParameter("@Email", SqlDbType.NVarChar) { Value = obj.Email };

            // cau query select
            string sql = "INSERT INTO tbl_NhaCungCap  (TenNhaCungCap, DiaChi, DienThoai, Email) VALUES (@TenNhaCungCap, @DiaChi, @DienThoai,@Email)";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 3. Xóa nha cung cap
        /// </summary>
        /// <param name="MaNhaCungCap">MaNhaCungCap INT</param>
        /// <returns>true</returns>
        public bool DeleteData(int MaNhaCungCap)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaNhaCungCap", SqlDbType.Int) { Value = MaNhaCungCap };
            // cau query select
            string sql = "DELETE FROM tbl_NhaCungCap WHERE MaNhaCungCap = @MaNhaCungCap";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 4. sửa nha cung cap
        /// </summary>
        /// <param name="obj">NhaCungCap</param>
        /// <returns>true</returns>
        public bool UpdateData(NhaCungCap obj)
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@TenNhaCungCap", SqlDbType.NVarChar) { Value = obj.TenNhaCungCap };
            param[1] = new SqlParameter("@DiaChi", SqlDbType.NVarChar) { Value = obj.DiaChi };
            param[2] = new SqlParameter("@DienThoai", SqlDbType.NVarChar) { Value = obj.DienThoai };
            param[3] = new SqlParameter("@Email", SqlDbType.NVarChar) { Value = obj.Email };
            param[4] = new SqlParameter("@MaNhaCungCap", SqlDbType.Int) { Value = obj.MaNhaCungCap };

            // cau query select
            string sql = "UPDATE tbl_NhaCungCap SET TenNhaCungCap = @TenNhaCungCap, DiaChi = @DiaChi, DienThoai = @DienThoai, Email = @Email  " +
                           " WHERE MaNhaCungCap = @MaNhaCungCap";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 5. Tìm theo ma nha cung cap
        /// </summary>
        /// <param name="MaNhaCungCap"></param>
        /// <returns></returns>
        public DataTable GetDataById(int MaNhaCungCap)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaNhaCungCap", SqlDbType.Int) { Value = MaNhaCungCap };
            // cau query select
            string sql = "SELECT * FROM tbl_NhaCungCap WHERE MaNhaCungCap = @MaNhaCungCap";
            DataTable dt = dataAccess.GetData(sql, param);

            return dt;
        }

        // 6. Login
        // 7. Logout
    }
}
